package com.xiaoyu.lanling.feature.moment.model

import `in`.srain.cube.request.JsonData
import android.text.TextUtils
import com.xiaoyu.lanling.widget.momentgrid.MomentGridItem

class Feed(val id: String,
           val text: String,
           val images: ArrayList<MomentGridItem>,
           val type: String,
           var likeCount: Int,
           var commentCount: Int,
           var liked: Boolean,
           val isOwner: Boolean,
           val address: String) {

    companion object {
        const val TYPE_TEXT = "text"
        const val TYPE_IMAGE = "images"

        fun fromJson(jsonData: JsonData): Feed {
            val id = jsonData.optString("id")
            val text = jsonData.optString("text")
            val type = jsonData.optString("type")
            val likeCount = jsonData.optInt("likeCount")
            val commentCount = jsonData.optInt("commentCount")
            val liked = jsonData.optBoolean("liked")
            val isOwner = jsonData.optBoolean("isOwner")
            val address = TextUtils.join(".", arrayOf(jsonData.optString("createTimeDes"), jsonData.optString("address")))
            val images = jsonData.optJson("images").asList<String>()
            val imageList = arrayListOf<MomentGridItem>()
            for (image in images) {
                imageList.add(MomentGridItem(image, ArrayList(images)))
            }
            return Feed(id, text, imageList, type, likeCount, commentCount, liked, isOwner, address)
        }
    }
}